package main

import (
	"C"
	"fmt"
	"github.com/google/go-github/github"
	"golang.org/x/net/context"
	"golang.org/x/oauth2"
	"img_bed/contents"
	"img_bed/def"
	"img_bed/utils"
	"log"
	"os"
)

var client *github.Client
var ctx *context.Context
var imgBedContents *contents.ImgBedContents

func init() {
	ctxTmp := context.Background()
	ts := oauth2.StaticTokenSource(
		&oauth2.Token{AccessToken: def.ACCESS_TOKEN},
	)
	tc := oauth2.NewClient(ctxTmp, ts)
	client = github.NewClient(tc)
	ctx = &ctxTmp
	imgBedContents = contents.New(client, ctx)
}

//export UploadFile
func UploadFile(fullPath *C.char) *C.char {
	tmpFullPath := C.GoString(fullPath)
	fileUrl := imgBedContents.UploadContent(&tmpFullPath)
	return C.CString(*fileUrl)
}

func main() {
	args := os.Args

	uploadFullFile := utils.SeparatorConvert(args[1])

	log.Printf("上传的文件为：%s", uploadFullFile)

	fileUrl := imgBedContents.UploadContent(&uploadFullFile)

	if fileUrl != nil {
		fmt.Println("fileUrl:=" + *fileUrl)
	}
}
